草庐IT

URL 重写对 SEO 的影响

全部标签

ruby - 从 href html 标签中使用 ruby​​ 中的 nokogiri 提取链接(URL)?

我想从网页中提取所有URL,如何使用nokogiri做到这一点?例子:site1site2site3resultshouldbeanlist:l=['http://example.org/site/1/','http://example.org/site/2/','http://example.org/site/3/' 最佳答案 你可以这样做:doc=Nokogiri::HTML.parse(site1site2site3site4site5site6HTML_ENDl=doc.css('div.heata').map{|link|

ruby-on-rails - 如何设置 ActionMailer default_url_options 的 :host dynamically to the request's hostname?

我正在尝试设置:hostforactionmailer默认url选项。我在所有环境文件中设置了以下内容config.action_mailer.default_url_options={:host=>"localhost"}我想通过提供请求主机使其更具动态性。当我尝试通过设置它时config.action_mailer.default_url_options={:host=>request.domain}或config.action_mailer.default_url_options={:host=>request.env["SERVER_NAME"]}它抛出错误...无法识别“请求

ruby - 在 Ruby 中从 URL 获取文件名的好方法是什么?

从以下位置提取filename.jpg的好方法是什么:url='http://www.example.com/foo/bar/filename.jpg?2384973948743'我正在使用Ruby1.9.3。 最佳答案 require'uri'url='http://www.example.com/foo/bar/filename.jpg?2384973948743'uri=URI.parse(url)putsFile.basename(uri.path)#=>filename.jpg

ruby-on-rails - 在 Rails 中,如何获取当前 URL(但没有路径)

如果我在一个URL中,例如http://domain.example/mysite/bla如何只请求没有路径的URL?比如http://domain.example 最佳答案 你可以用这个#=>https://domain.example:3000#=>https://domain.example从Rails3.2开始你也可以使用#=>https://domain.example:3000 关于ruby-on-rails-在Rails中,如何获取当前URL(但没有路径),我们在Stack

ruby - 在 Ruby 中重写 to_s 方法不好吗?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭7年前。Improvethisquestion我一直在试验,发现我喜欢重新定义Object的to_s方法。这是一个坏主意还是一个好的做法?

ruby-on-rails - 如何在 Ruby 中给定 URL 中的 base64 编码媒体

我正在尝试将图像上传到PingFM。他们的documentation说:media–base64encodedmediadata.我可以通过URL访问此图像。我试过(几乎猜到了)这个:ActiveSupport::Base64.encode64(open("http://image.com/img.jpg"))但是我得到这个错误:TypeError:can'tconvertTempfileintoStringfrom/usr/lib/ruby/1.8/base64.rb:97:in`pack'from/usr/lib/ruby/1.8/base64.rb:97:in`encode64'

ruby - 为什么显式返回会影响 Proc?

deffoof=Proc.new{return"returnfromfoofrominsideproc"}f.call#controlleavesfooherereturn"returnfromfoo"enddefbarb=Proc.new{"returnfrombarfrominsideproc"}b.call#controlleavesbarherereturn"returnfrombar"endputsfoo#prints"returnfromfoofrominsideproc"putsbar#prints"returnfrombar"我以为return关键字在Ruby中是可选的

ruby - 检查 URL 是否存在于 Ruby 中

我如何使用Ruby检查URL是否存在?例如,对于URLhttps://google.com结果应该是真实的,但是对于URLhttps://no.such.domain或https://stackoverflow.com/no/such/path结果应该是假 最佳答案 使用Net::HTTP图书馆。require"net/http"url=URI.parse("http://www.google.com/")req=Net::HTTP.new(url.host,url.port)res=req.request_head(url.pat

ruby-on-rails - 如何从 Rails 中的 URL 获取查询字符串

有没有办法在Rails中获取传递的URL字符串中的查询字符串?我想传递一个URL字符串:http://www.foo.com?id=4&empid=6如何获取id和empid? 最佳答案 如果您在字符串中有一个URL,则使用URI和CGI​​将其分开:url='http://www.example.com?id=4&empid=6'uri=URI.parse(url)params=CGI.parse(uri.query)#paramsisnow{"id"=>["4"],"empid"=>["6"]}id=params['id'].f

ruby-on-rails - 解析字符串以添加到 URL 编码的 URL

给定字符串:"Hellothereworld"如何创建这样的URL编码字符串:"Hello%20there%20world"我还想知道如果字符串也有其他符号该怎么办,比如:"hellothere:world,howareyou"最简单的方法是什么?我打算解析然后为此构建一些代码。 最佳答案 在2019年,URI.encode已过时,不应再使用。require'uri'URI.encode("Hellothereworld")#=>"Hello%20there%20world"URI.encode("hellothere:world,